#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import platform
import socket
import urllib2
import sys
import subprocess
import gettext
import uuid
import string
import os
import json

class InformationNB:
	def getNombrePc(self):
		try:
			pc_name = socket.gethostname()
			
			if pc_name != "":
				return pc_name
			else:
				return "No data"
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"

	def getSistemaOperativo(self):
		try:
			sisop = platform.uname()[0] + " " + platform.uname()[2] + " v" + platform.uname()[3]
			if sisop != "":
				return sisop
			else:
				return "No data" 
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"

	def getArquitecturaCpu(self):
		try:
			arch_cpu = platform.uname()[4]
			if arch_cpu != "":
				return arch_cpu
			else:
				return "No data" 
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"

	def getIpInterna(self):
		try:
			ip_local_add = socket.gethostbyname(socket.gethostname())
			if ip_local_add != "":
				return ip_local_add
			else:
				return "No data"  
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"

	def getIpExterna(self):
		try:
			ip_add_ext = urllib2.urlopen("http://www.whatismyip.com/automation/n09230945.asp").read()
			#direcion para obtener ip externa sera cambiada por una propia en php
			if ip_add_ext != "":
				return ip_add_ext
			else:
				return "No data" 
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"
		
	def getMac(self):
		try:
			node = uuid.getnode()
			mac = uuid.UUID(int=node)
			addr_ot = str(mac.hex[-12:])
			addr = addr_ot[0:2]+"-"+addr_ot[2:4]+"-"+addr_ot[4:6]+"-"+addr_ot[6:8]+"-"+addr_ot[8:10]+"-"+addr_ot[10:12]
			addr = string.upper(addr)
			if addr != "":
				return addr
			else:
				return "No data" 
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"

	def getListadoProcesos(self):
		try:
			if platform.uname()[0] == "Windows":
				return subprocess.check_output("tasklist")
			else:
				return os.system("ps aux")
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"

	def getListadoConexiones(self):
		try:
			if platform.uname()[0] == "Windows":
				return subprocess.check_output("netstat -a")
			else:
				return os.system("netstat -a")
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"
			
	def getInfoRed(self): #para usuarios de paga
		try:
			if platform.uname()[0] == "Windows":
				return subprocess.check_output("ipconfig /all")
			else:
				return os.system("ifconfig -a")
		except:
			print "Problemas al entregar informacion de sistema"
			return "No data"
			
	def getGeolocation(self): #latitude, longitude, altitude, accuracy, altitude_accuracy, address
		try:
			try:
				if platform.uname()[2] == "XP":
					jsonString = subprocess.check_output("wifxp.exe")
				else:
					jsonString = subprocess.check_output("wif.exe")
			except:
				jsonString = ""
			
			urlJson = 'http://www.google.com/loc/json'
			jsonData = '{ "version": "1.1.0", "request_address": "true"'+jsonString+' }'
			r=urllib2.Request(urlJson, jsonData)
			
			jdata = json.loads(urllib2.urlopen(r, timeout = 90).read())
			
			if jdata['location']['latitude'] == "" or jdata['location']['longitude'] == "":
				print 'Google maps WebService Error'
				data = {"latitude":0, "longitude":0}
			else:
				return jdata['location']
		except:
			print 'Google maps WebService Error'
			data = {"latitude":0, "longitude":0}
			return data